package com.itheima.edu.info.manager.entry;

import com.itheima.edu.info.manager.controller.StudentController;
import com.itheima.edu.info.manager.controller.TeacherController;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.PrimitiveIterator;
import java.util.Properties;
import java.util.Scanner;

public class InfoManagerEntry {
    static final String PATH = "itheima-manager-standard-edition\\user.properties";
    public static void main(String[] args) {
        login();
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("--------欢迎来到黑马信息管理系统--------");
            System.out.println("请输入您的选择: 1.学生管理  2.老师管理  3.退出");
            String choice = sc.next();
            switch (choice){
                case "1":   //进入学生管理系统
                    StudentController studentController = new StudentController();
                    studentController.start();
                    break;
                case "2":   //进入教师管理系统
                    TeacherController teacherController = new TeacherController();
                    teacherController.start();
                    break;
                case "3":   //退出主系统
                    System.exit(0); //退出当前运行的jvm虚拟机
                    break;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    private static void login() {
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i <= 3; i++) {
            System.out.println("请输入用户名:");
            String name = sc.nextLine();
            System.out.println("请输入用户密码:");
            String pwd = sc.nextLine();

            //获取配置文件中的用户信息
            try {
                //创建字符输入流对象
                FileReader fr = new FileReader(PATH);
                //创建properties对象
                Properties prop = new Properties();
                prop.load(fr);
                //判断用户信息是否正确
                if (prop.containsKey(name) && prop.getProperty(name).equals(pwd)){
                    break;
                }else {
                    if (i == 3){
                        System.out.println("本日次数已用尽,请联系管理员解除风控");
                        System.exit(0);
                    }
                    System.out.println("输入的用户名或密码错误,还有" + (3-i) + "次尝试机会");
                }

            } catch (FileNotFoundException e) {
                //e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
